import * as API from '@/api'
import { getNextOrPrevDate } from '@/utils/tools'

// type : 成员 authPass 进件 reward
const passSum = (data, type) => {
  let time = ''
  let monthList = []
  while(monthList.length < 3) {
    time = getNextOrPrevDate(time)
    monthList.push(time)
  }
  let sum = 0
  for (let i = 0; i < monthList.length; i++) {
    sum += Number(data[type][monthList[i]])
  }
  sum += Number(data[type]['orther'])
  return sum || 0
}

const state = {
  teamInputSummary: {
    reward: {
      orther: 0,
    },
    successSum: 0,
    noRewardSum: 0,
    inProgressSum: 0,
    sum: 0,
    todayRewardSum: 0,
  },
  memberSummary: {
    authPass: {
      orther: 0,
    },
    passSum: 0,
    pendingSum: 0,
    rejectSum: 0,
    teamSum: 0,
  },
};

// 获取state的数据
const getters = {
  teamInputSummary: (state) => state.teamInputSummary,
  memberSummary: (state) => state.memberSummary,
};

// 更新state的数据
const mutations = {
  TeamInputSummary(state, data) {
    state.teamInputSummary = data;
  },
  MemberSummary(state, data) {
    state.memberSummary = data;
  },
};

// 更新state数据的动作
const actions = {
  async getMemberSummary({ commit }, params) {
    try {
      const res = await API.MemberSummary(params);
      if (res && JSON.stringify(res) !== '{}'){
        res.passSum = passSum(res, 'authPass')
        commit('MemberSummary', res)
      }
    } catch (e) {
    }
  },
  async getTeamInputSummary({ commit }, params) {
    try {
      const res = await API.TeamInputSummary(params);
      if (res && JSON.stringify(res) !== '{}'){
        res.successSum = passSum(res, 'reward')
        commit("TeamInputSummary", res)
      }
    } catch (e) {
    }
  },
};

export default { state, getters, mutations, actions };
